﻿using A.M.ETO;
using A.M.Storage.Application;
using A.M.Storage.I_Infrastruecure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using W.M.Materiel.Application.IServices;
using W.M.Materiel.Domain.Entity;
using W.M.Materiel.Infrastruecure.IRepository;
using W.M.Materiel.Infrastruecure.Repository;

namespace W.M.Materiel.Application.Services
{
    internal class Bom_list_propertiesServices : BaseServices<bom_list_properties, string>, IBom_list_propertiesServices
    {
        private readonly IBom_list_propertiesRepository bom_List_PropertiesRepository;

        public Bom_list_propertiesServices(IBom_list_propertiesRepository bom_List_PropertiesRepository) : base(bom_List_PropertiesRepository)
        {
            this.bom_List_PropertiesRepository = bom_List_PropertiesRepository;
        }

        /// <summary>
        /// 显示物料属性分页+查询
        /// </summary>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="name">查询条件:物料属性名称</param>
        /// <returns>统一返回格式</returns>
        public async Task<PageETO<bom_list_properties>> GetBom_list_properties(int pageSize,int pageIndex,string? name)
        {
            PageETO<bom_list_properties> pageETO = new PageETO<bom_list_properties>();
            var list = await bom_List_PropertiesRepository.GetShowInforAsync();
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(x => x.material_attribute_name.Contains(name)).ToList();
            }
            pageETO.TotalCount = list.Count;
            pageETO.PageCount = (int)Math.Ceiling(pageETO.TotalCount * 1.0 / pageSize);
            pageETO.Data = list.Skip((pageIndex-1)*pageSize).Take(pageSize).ToList();

            return pageETO;
        }

        /// <summary>
        /// 显示物料类别
        /// </summary>
        /// <returns></returns>
        public async Task<List<bom_list_properties>> GetBom_List_Categories()
        {
            var list = await bom_List_PropertiesRepository.GetShowInforAsync();
            return list;
        }


        /// <summary>
        /// 添加物料属性
        /// </summary>
        /// <param name="c">物料属性实体</param>
        /// <returns>统一返回格式</returns>
        public async Task<ApiReturn<string>> GetBom_list_propertiesAdd(bom_list_properties c)
        {
            ApiReturn<string> apiReturn=new ApiReturn<string>();
            var list=await bom_List_PropertiesRepository.AddInfor_Async(c);
            if (list > 0)
            {
                apiReturn.Code= 200;
                apiReturn.Message = "添加成功!";
            }
            else
            {
                apiReturn.Code = 400;
                apiReturn.Message = "添加失败!";
            }
            return apiReturn;
        }

        /// <summary>
        /// 删除物料属性信息
        /// </summary>
        /// <param name="c">物料属性编号</param>
        /// <returns>统一返回格式</returns>
        public async Task<ApiReturn<int>> GetBom_list_propertiesDel(string? c)
        {
            ApiReturn<int> apiReturn=new ApiReturn<int>();
            var res =await bom_List_PropertiesRepository.GetShowObjAsync(x => x.material_attribute_number == c);
            var list =await bom_List_PropertiesRepository.DelInfor_Async(res);
            if (list != null)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "删除成功!";
            }
            else
            {
                apiReturn.Code = 400;
                apiReturn.Message = "删除失败!";
            }
            return apiReturn;
        }

        /// <summary>
        /// 修改物料属性信息
        /// </summary>
        /// <param name="c">物料属性实体</param>
        /// <returns>统一返回格式</returns>
        public async Task<ApiReturn<string>> GetBom_list_propertiesUpdate(bom_list_properties c)
        {
            ApiReturn<string> apiReturn=new ApiReturn<string>();
            var res =await bom_List_PropertiesRepository.GetShowInforAsync(x => x.material_attribute_number.Equals( c.material_attribute_number));
            foreach (var item in res)
            {
                item.material_attribute_number = c.material_attribute_number;
                item.material_attribute_name = c.material_attribute_name;
                item.status = c.status;
                item.operating_time = c.operating_time;
                item.operators = c.operators;
                item.remark = c.remark;
            }
            var list =await bom_List_PropertiesRepository.UpdInfor_Batch_Async(res);
            if (list > 0)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "修改成功!";
            }
            else
            {
                apiReturn.Code = 400;
                apiReturn.Message = "修改失败!";
            }
            return apiReturn;
        }

        /// <summary>
        /// 反填物料属性信息
        /// </summary>
        /// <param name="material_attribute_number">物料属性编号</param>
        /// <returns>物料属性信息</returns>
        public async Task<bom_list_properties> GetbackfillProperties(string? material_attribute_number)
        {
            Expression<Func<bom_list_properties, bool>> wherename = x => x.material_attribute_number == material_attribute_number;
            var list =await bom_List_PropertiesRepository.GetShowObjAsync(wherename);
            return list;
        }
    }
}
